﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crosland.Automotriz.Core;
namespace Crosland.Automotriz.Data.Dao.Linq
{
    public class clsT_Mae_Tabla_General_CabDAO
    {

        private AutomotrizEntities ContextoDeObjs = new AutomotrizEntities();

        public IEnumerable<Object> lista() {

            IEnumerable<Object> list = new List<Object>();

            try
            {

                var q = from t in ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB
                        from m in ContextoDeObjs.T_MAE_MODULO
                        where t.ID_MODULO == m.ID_MODULO
                        select new
                        {
                            t.ID_GENERAL_CAB,
                            t.NOM_TABLA,
                            t.DES_OBSERVACIONES,
                            t.ID_MODULO,
                            t.IND_ACTIVO,
                            m.NOM_MODULO
                        };



                list = q.ToList();
            }
            catch{}

            return list;

        
        }




        public bool Insertar(T_MAE_TABLA_GENERAL_CAB Obj)
        {
            try
            {
                ContextoDeObjs.AddObject("T_MAE_TABLA_GENERAL_CAB", Obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }


        public bool Modificar(T_MAE_TABLA_GENERAL_CAB obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_MAE_TABLA_GENERAL_CAB", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;

            }
            return true;
        }



        public bool Eliminar(T_MAE_TABLA_GENERAL_CAB obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_MAE_TABLA_GENERAL_CAB", obj);
                ContextoDeObjs.DeleteObject(obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }



        public int UltimoNumero()
        {
            int max = 0;

            try
            {
                max = ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB.Select(x => x.ID_GENERAL_CAB).Max();

            }
            catch (Exception)
            {
                return max;
            }

            return max;

        }


        public int ListarFiltroNombre(string nombre,int id_modulo) {

            List<T_MAE_TABLA_GENERAL_CAB> list = new List<T_MAE_TABLA_GENERAL_CAB>();           
                var q = from c in ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB
                        where c.NOM_TABLA==nombre && c.ID_MODULO==id_modulo
                        select c;
                list = q.ToList();
            return list.Count();       
        }

    }
}
